package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.HomeClassBeen;
import cn.xfdzx.android.apps.shop.bean.NearShopBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.view.MyGridView;
import cn.xfdzx.android.apps.shop.view.MyRatingBar;

import static cn.xfdzx.android.apps.shop.apps.Constants.SHOPID_69_NO;

/**
 * @author gzk
 * @date 2020/4/6 0006
 * description：
 */
public class HomeNearShopAdapter extends RecyclerView.Adapter<HomeNearShopAdapter.Holder> {

    private Context mContext;
    private List<NearShopBeen.DataBean.DataListBean> mList;
    private ItemClickListener mItemClickListener;
    private List<NearShopBeen.DataBean.DataListBean.CouponBean> coupon = new ArrayList<>();
    RoundedCorners roundedCorners;
    RequestOptions options;
    public HomeNearShopAdapter(Context mContext) {
        this.mContext = mContext;
         roundedCorners = new RoundedCorners(20);
          options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
    }

    @Override
    public HomeNearShopAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_home_shop_list, null);

        Holder holder = new Holder(view);
        return holder;
    }

    public void setmList(List<NearShopBeen.DataBean.DataListBean> mList) {
        this.mList = mList;
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        holder.tvHomeShopListTitle.setText(mList.get(position).getOffline_shop_name());

        holder.tvShunfeng.setText(mList.get(position).getExpresses_name());

        if (mList.get(position).getIs_close()==0){
            holder.tvDayang.setVisibility(View.GONE);
        }else {
            holder.tvDayang.setVisibility(View.VISIBLE);
        }

        if (UtilsData.getPreference(mContext,"activity").getBoolean("is_69")&&!SHOPID_69_NO.contains(mList.get(position).getId())){
            holder.tvHomeManJian.setVisibility(View.VISIBLE);
        }else {
            holder.tvHomeManJian.setVisibility(View.GONE);
        }
        Glide.with(mContext).load(mList.get(position).getLogo()).apply(options.transform(new CenterCrop(), roundedCorners)).into(holder.ivHomeShopList);

        holder.tvHomeShopListLevel.setText(mList.get(position).getStar() + "");
        holder.rbStartLevel.setRating(mList.get(position).getStar());
        holder.tvHomeShopListMonthSales.setText("月销" + mList.get(position).getMonthly_sales());
        holder.tvHomeShopListUpSend.setText("起送¥" + mList.get(position).getSend_fees());
        holder.tvHomeShopListBasis.setText("基础运费¥" + mList.get(position).getBase_freight());

        holder.tvHomeShopListSendDistance.setText(mList.get(position).getDistance() +"");

        if (EmptyUtil.isNotEmpty(mList.get(position).getCoupon())){
            coupon = mList.get(position).getCoupon();
        }
        holder.mgvHomeShopList.setAdapter(new HomeShopJuanGVAdapter(mContext, coupon));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    public class Holder extends RecyclerView.ViewHolder {

        private TextView tvHomeManJian;
        private TextView tvHomeShopListTitle;
        private TextView tvShunfeng;
        private TextView tvDayang;//打烊
        private MyRatingBar rbStartLevel;
        private ImageView ivHomeShopList;
        private TextView tvHomeShopListLevel;
        private TextView tvHomeShopListMonthSales;
        private TextView tvHomeShopListUpSend;
        private TextView tvHomeShopListBasis;
        private TextView tvHomeShopListSendDistance;
        private TextView tvHomeShopListSendTime;
        private MyGridView mgvHomeShopList;
        private MyGridView mgvHomeGoodsList;

        public Holder(View itemView) {
            super(itemView);
            tvHomeManJian = itemView.findViewById(R.id.tv_home_man_jian);
            tvHomeShopListTitle = itemView.findViewById(R.id.tv_home_shop_list_title);
            tvShunfeng = itemView.findViewById(R.id.tv_shunfeng);
            tvDayang = itemView.findViewById(R.id.tv_dayang);
            rbStartLevel = itemView.findViewById(R.id.rb_start_level);
            ivHomeShopList = itemView.findViewById(R.id.iv_home_shop_list);
            tvHomeShopListLevel = itemView.findViewById(R.id.tv_home_shop_list_level);
            tvHomeShopListMonthSales = itemView.findViewById(R.id.tv_home_shop_list_month_sales);
            tvHomeShopListUpSend = itemView.findViewById(R.id.tv_home_shop_list_up_send);
            tvHomeShopListBasis = itemView.findViewById(R.id.tv_home_shop_list_basis);
            tvHomeShopListSendDistance = itemView.findViewById(R.id.tv_home_shop_list_send_distance);
            tvHomeShopListSendTime = itemView.findViewById(R.id.tv_home_shop_list_send_time);
            mgvHomeShopList = itemView.findViewById(R.id.mgv_home_shop_list);
            mgvHomeGoodsList = itemView.findViewById(R.id.mgv_home_goods_list);
        }
    }

    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }


}
